#include "Game.h"
#include "KeyboardPlayerInput.h"
#include "Keyboard.h"
#include <iostream>

Game::Game(void):
	width(500),
	height(300),
	bpp(32)
{
	spaceKeyWasPressed = false;
	
	gamemode = new GameMode();
	*gamemode = GM_INTRO;

	keyboard = 0;
	leftPlayer = 0;
	rightPlayer = 0;
	ball = 0;
	splashscreen = 0;
}


Game::~Game(void)
{
	delete splashscreen;
	delete keyboard;
	delete leftPlayer;
	delete rightPlayer;
	delete ball;
	delete gamemode;
}

void Game::drawObjects()
{}

void Game::update(double timePassed)
{
   	if(keyboard->keyDown(KEY_SPACE))
		spaceKeyWasPressed = true;

	if(spaceKeyWasPressed && !keyboard->keyDown(KEY_SPACE))
	{
		spaceKeyWasPressed = false;

		// switch mode from pauzed to play or from play to pauze
		if(*gamemode == GM_PAUZED)
			*gamemode = GM_PLAY;
		else if(*gamemode == GM_PLAY)
			*gamemode = GM_PAUZED;
	}



	if(*gamemode == GM_PAUZED)
	{
		return;
	}

	if(*gamemode == GM_INTRO)
	{
		if(leftPlayer->canComeOutOfIntro() && rightPlayer->canComeOutOfIntro())
			*gamemode = GM_PLAY;
	}

	if(*gamemode == GM_PLAY || *gamemode == GM_INTRO)
		updatePlay(timePassed);


	if(*gamemode == GM_SPLASH)
		updateSplash(timePassed);

	std::cerr << "Time passed: \t" << timePassed << ",\t that's " << 1/timePassed << " fps.\n";

}

void Game::updateSplash(double timePassed)
{


}


void Game::updatePlay(double timePassed)
{
	leftPlayer->update(timePassed);
	rightPlayer->update(timePassed);
	ball->update(timePassed);

	if(ball->hasScored())
	{
		PlayerPosition scoringPlayer = ball->whoScored();
		scorecounter->scoredPoint(scoringPlayer);

		leftPlayer->reset();
		rightPlayer->reset();
		ball->reset();


	}
}

